package com.ruoyi.erp.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.erp.ErpTargetTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ErpAnnualTargetMapper;
import com.ruoyi.erp.domain.ErpAnnualTarget;
import com.ruoyi.erp.service.IErpAnnualTargetService;

/**
 * 年度目标Service业务层处理
 *
 * @author yanwenliang
 * @date 2024-10-17
 */
@Service
public class ErpAnnualTargetServiceImpl extends ServiceImpl<ErpAnnualTargetMapper, ErpAnnualTarget> implements IErpAnnualTargetService
{
    @Autowired
    private ErpAnnualTargetMapper erpAnnualTargetMapper;

    /**
     * 查询年度目标
     *
     * @param targetId 年度目标主键
     * @return 年度目标
     */
    @Override
    public ErpAnnualTarget selectErpAnnualTargetByTargetId(String targetId)
    {
        return erpAnnualTargetMapper.selectErpAnnualTargetByTargetId(targetId);
    }

    /**
     * 查询年度目标列表
     *
     * @param erpAnnualTarget 年度目标
     * @return 年度目标
     */
    @Override
    public List<ErpAnnualTarget> selectErpAnnualTargetList(ErpAnnualTarget erpAnnualTarget) {

        if (!SecurityUtils.hasAdmin()){
            erpAnnualTarget.setUserId(SecurityUtils.getUserId());
        }

        return erpAnnualTargetMapper.selectErpAnnualTargetList(erpAnnualTarget);
    }

    /**
     * 新增年度目标
     *
     * @param erpAnnualTarget 年度目标
     * @return 结果
     */
    @Override
    public int insertErpAnnualTarget(ErpAnnualTarget erpAnnualTarget) {
        String groupId = SecurityUtils.getLoginUser().getUser().getGroupId();
        Long deptId = SecurityUtils.getLoginUser().getUser().getDeptId();
        Integer integer = erpAnnualTargetMapper.selectCount(new LambdaQueryWrapper<ErpAnnualTarget>()
                .eq(ErpAnnualTarget::getYear, erpAnnualTarget.getYear())
                .eq(ErpAnnualTarget::getUserId, SecurityUtils.getUserId())
                .eq(ErpTargetTypeEnum.USER.getCode().equals(erpAnnualTarget.getTargetType()), ErpAnnualTarget::getTargetType, ErpTargetTypeEnum.USER.getCode())
                .eq(ErpTargetTypeEnum.GROUP.getCode().equals(erpAnnualTarget.getTargetType()), ErpAnnualTarget::getTargetType, ErpTargetTypeEnum.GROUP.getCode())
                .eq(ErpTargetTypeEnum.DEPT.getCode().equals(erpAnnualTarget.getTargetType()), ErpAnnualTarget::getTargetType, ErpTargetTypeEnum.DEPT.getCode()));

        if (integer > 0){
            throw new ServiceException("该类型的年度目标已经提交过，无法二次提交");
        }

        erpAnnualTarget.setCreateTime(DateUtils.getNowDate());
        erpAnnualTarget.setCreateBy(SecurityUtils.getUsername());
        erpAnnualTarget.setTargetId(IdWorker.getIdStr());
        erpAnnualTarget.setUserId(SecurityUtils.getUserId());


        if (ErpTargetTypeEnum.DEPT.getCode().equals(erpAnnualTarget.getTargetType())){
            erpAnnualTarget.setDeptId(deptId);
        } else if (ErpTargetTypeEnum.GROUP.getCode().equals(erpAnnualTarget.getTargetType())){
            erpAnnualTarget.setGroupId(groupId);
        }

        if (!ErpTargetTypeEnum.USER.getCode().equals(erpAnnualTarget.getTargetType())
                && ObjectUtils.isEmpty(erpAnnualTarget.getDeptId())
                && StringUtils.isEmpty(erpAnnualTarget.getGroupId())){
            throw new ServiceException("你不属于任何部门或者小组，无法设置部门或者小组目标");
        }

        return erpAnnualTargetMapper.insertErpAnnualTarget(erpAnnualTarget);
    }

    /**
     * 修改年度目标
     *
     * @param erpAnnualTarget 年度目标
     * @return 结果
     */
    @Override
    public int updateErpAnnualTarget(ErpAnnualTarget erpAnnualTarget) {
        erpAnnualTarget.setUpdateTime(DateUtils.getNowDate());
        erpAnnualTarget.setUpdateBy(SecurityUtils.getUsername());
        return erpAnnualTargetMapper.updateErpAnnualTarget(erpAnnualTarget);
    }

    /**
     * 批量删除年度目标
     *
     * @param targetIds 需要删除的年度目标主键
     * @return 结果
     */
    @Override
    public int deleteErpAnnualTargetByTargetIds(String[] targetIds)
    {
        return erpAnnualTargetMapper.deleteErpAnnualTargetByTargetIds(targetIds);
    }

    /**
     * 删除年度目标信息
     *
     * @param targetId 年度目标主键
     * @return 结果
     */
    @Override
    public int deleteErpAnnualTargetByTargetId(String targetId)
    {
        return erpAnnualTargetMapper.deleteErpAnnualTargetByTargetId(targetId);
    }
}
